package com.hsh;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.hsh.WordCount.IntSumReducer;
import com.hsh.WordCount.TokenizerMapper1;

public class WordCountMain {
	public static void main(String[] args) throws Exception {
//		Path input = new Path("F:/workspace/LocalMR/Input");
		Path input = new Path("hdfs://192.168.1.2:9000/usr/root/input/");
	//	File outPutDir = new File("hdfs://192.168.59.129:9000/");
	//	File outPutDir = new File("F:/workspace/LocalMR/Output");
	//	CommonUtils.deleteDir(outPutDir);
	//	if(outPutDir.exists())
	//		outPutDir.delete();
		Path output = new Path("hdfs://192.168.1.2:9000/wordcountresult0002");
		Configuration conf = new Configuration();
		//conf.setStrings("hadoop.tmp.dir", "d://tmp");
	//	conf.setInt("mapred.map.tasks", 10);
		conf.set("mapred.job.tracker", "192.168.1.2:9001");
		conf.setBoolean("dfs.permissions", true);
	//	conf.setInt("mapred.reduce.tasks",3);
//		conf.set("tmpjars","C:/Users/Administrator/Desktop/Script2-1.0.jar");
		conf.set("mapred.jar","c://WC.jar");
		Job job = new Job(conf, "word-+++-count");
		job.setNumReduceTasks(3);
	//	job.setJarByClass(WordCount.class);
		
		job.setInputFormatClass(TextInputFormat.class);
		job.setMapperClass(TokenizerMapper1.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		FileInputFormat.addInputPath(job, input);
		FileOutputFormat.setOutputPath(job, output);

		System.out.println(conf.get("mapred.map.tasks"));
		System.out.println(conf.get("mapred.reduce.tasks"));
		System.out.println(conf.get("mapred.job.tracker"));
		System.out.println(conf.get("mapred.jobtracker.taskScheduler.maxRunningTasksPerJob"));
		long begTime = System.currentTimeMillis();
		boolean flag = job.waitForCompletion(true);
		long endTime = System.currentTimeMillis();
		System.out.println("---耗时---" + (endTime - begTime)/1000);
		if(flag)
			System.exit(1);
		System.exit(0);
	}
}
